package person.aoao.library.controller;

import person.aoao.library.entity.BannerList;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.BannerListService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (BannerList)表控制层
 *
 * @author makejava
 * @since 2023-05-05 10:15:41
 */
@RestController
@RequestMapping("bannerList")
@CrossOrigin
public class BannerListController {
    /**
     * 服务对象
     */
    @Resource
    private BannerListService bannerListService;

    /**
     * 分页查询
     *
     * @param bannerList  筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<BannerList>> queryByPage(BannerList bannerList, PageRequest pageRequest) {
        return ResponseEntity.ok(this.bannerListService.queryByPage(bannerList, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<BannerList> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.bannerListService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param bannerList 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<BannerList> add(BannerList bannerList) {
        return ResponseEntity.ok(this.bannerListService.insert(bannerList));
    }

    /**
     * 编辑数据
     *
     * @param bannerList 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<BannerList> edit(BannerList bannerList) {
        return ResponseEntity.ok(this.bannerListService.update(bannerList));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.bannerListService.deleteById(id));
    }


    @GetMapping("/getBanner/{num}/{page}")
    public ResponseData getBannner(@PathVariable Integer num,@PathVariable  Integer page){
        ResponseData list=this.bannerListService.getBanner(num,page);
        return list;
    }

    @PostMapping("/addBanner")
    public  ResponseData addBanner(@RequestBody BannerList bannerList){
        ResponseData data=this.bannerListService.addBanner(bannerList);
        return  data;
    }

    @PostMapping("/editBanner")
    public ResponseData editBanner(@RequestBody BannerList bannerList){
        ResponseData data=this.bannerListService.editBanner(bannerList);
        return data;
    }

    @GetMapping("deleteBanner/{lid}")
    public ResponseData deleteBanner(@PathVariable Integer lid){
        ResponseData data=this.bannerListService.deleteBanner(lid);
        return  data;
    }

    @PostMapping("searchBanner/{state}/{num}/{page}")
    public ResponseData searchBanner(@RequestBody String value,@PathVariable String state,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.bannerListService.searchBanner(value.replace("\"", ""),state,num,page);
        return  data;
    }
}

